package org.september.pisces.sms.service;

import org.september.core.exception.BusinessException;
import org.september.pisces.settings.service.PiscesSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 移动云mas平台(云阳项目)
 * https://mas.10086.cn/
 */
@Service
public class MobileMasService implements GeneralSmsService{

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private PiscesSettingService settingService;
	
//	Client client = Client.getInstance();
	
	private boolean loginStatus = false;
	
	private String account;//用户名
	private String pwd; //密码
	private String ecName;//企业名称
	private String sign;//签名
	private String yunyangSmsLoginUrl;
	private String yunyangSmsSenderUrl;
	
	@Override
	public boolean send(String[] tels, String content) {
		if(loginStatus==false) {
			doLogin();
		}
		if(loginStatus==false) {
			throw new BusinessException("短信平台信息错误，发送短信失败");
		}
		String mobiles = String.join(",", tels);
		// 普通短信
		try {
			String serial="";
//			int rt = client.sendDSMS( tels, content, serial, 1, sign, null, true );
//			logger.info(mobiles+",短信发送结果:"+rt+",content="+content);
//			if(rt==1) {
//				return true;
//			}
			return false;
		}catch(Exception ex) {
			logger.info(mobiles+",短信发送失败"+",content="+content , ex);
			return false;
		}
	}

	public void init() {
		account = settingService.getValueOrDefault("移动云短信设置","mobile_mas_account","","移动云mas账号","");
		pwd = settingService.getValueOrDefault("移动云短信设置","mobile_mas_pwd","","移动云mas密码","");
		sign = settingService.getValueOrDefault("移动云短信设置","mobile_mas_sign","","移动云mas签名","");
		ecName = settingService.getValueOrDefault("移动云短信设置","mobile_mas_ec_name","","移动云mas企业名称","");
		yunyangSmsLoginUrl = settingService.getValueOrDefault("移动云短信设置","yunyang_mobile_mas_login_url","","移动云mas登录接口","");
		yunyangSmsSenderUrl = settingService.getValueOrDefault("移动云短信设置","yunyang_mobile_mas_sender_url","","移动云mas发送接口","");
		
//		account = "zhichao";
//		pwd = "Zfzd@1234560";
//		ecName="云阳县交通运输综合行政执法支队";
//		sign = "EpV6iY7po";
//		yunyangSmsLoginUrl = "http://112.35.4.197:15000";
//		yunyangSmsSenderUrl = "http://112.35.10.201:19999";
	}
	
	private void doLogin() {
		//TODO 依赖包问题
//		loginStatus = client.login(yunyangSmsLoginUrl, account, pwd, ecName);
//		if( loginStatus ) {
//			client.setSendUrl(yunyangSmsSenderUrl);
//			System.out.println( "Login successed" );
//		} else {
//			System.out.println( "Login failed" );
//		}
	}

	public static void main(String[] args) {
		MobileMasService mms = new MobileMasService();
		mms.init();
		mms.send(new String[] {"18768445463"}, "治超测试短信内容");
	}
}
